package com.promis.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by jidanbo on 2017/12/13.
 */

public class OvalConcaveTopView extends View {

    private int mArcHeight;

    private Context mContext;

    public OvalConcaveTopView(Context context) {
        super(context);
        init(context, null);
    }

    public OvalConcaveTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public OvalConcaveTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public OvalConcaveTopView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OvalConcaveTopView);
            mArcHeight = typedArray.getDimensionPixelSize(R.styleable.OvalConcaveTopView_arcHeight, 0);
        }

        mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, getHeight());
        path.quadTo(getWidth() / 2, getHeight() - 2 * mArcHeight, getWidth(), getHeight());
        path.lineTo(getWidth(), 0);
        path.close();
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
}
